package salary

type ChangeEmployeeTransaction struct {
	EmployeeId int
	Change     func(e Employee)
}

func GetChangeEmployeeTransaction(employeeId int) *ChangeEmployeeTransaction {
	return &ChangeEmployeeTransaction{
		EmployeeId: employeeId,
	}
}

func (c *ChangeEmployeeTransaction) Execute() {
	e := GetDB().GetEmployee(c.EmployeeId)
	if e.EmployeeId != 0 {
		c.Change(e)
	}
}

// 改名事务
type ChangeNameTransaction struct {
	ChangeEmployeeTransaction
	Name string
}

func GetChangeNameTransaction(employeeId int, name string) *ChangeNameTransaction {
	n := &ChangeNameTransaction{
		*GetChangeEmployeeTransaction(employeeId),
		name,
	}
	n.ChangeEmployeeTransaction.Change = n.Change
	return n
}

func (c *ChangeNameTransaction) Change(e Employee) {
	e.Name = c.Name
}


// 修改address事务
type ChangeAddressTransaction struct {
	ChangeEmployeeTransaction
	Address string
}

func GetChangeAddressTransaction(employeeId int, address string) *ChangeAddressTransaction {
	n := &ChangeAddressTransaction{
		*GetChangeEmployeeTransaction(employeeId),
		address,
	}
	n.ChangeEmployeeTransaction.Change = n.Change
	return n
}

func (c *ChangeAddressTransaction) Change(e Employee) {
	e.Address = c.Address
}

// 删除员工事务
type DeleteEmployeeTransaction struct {
	EmployeeId int
}

func (d DeleteEmployeeTransaction) Execute() {
	GetDB().RemoveEmployee(d.EmployeeId)
}

